package com.beerbook;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BeerDB extends SQLiteOpenHelper{
	private static final int VERSION = 1;
	private static final String DATABASE_NAME="beerbook.db";
	static final String _id = "ID";
	static final String beer = "Beer";
	static final String name = "BeerName";
	static final String brewery = "BreweryName";
	static final String category = "Category";
	static final String style = "Style";
	static final String abv = "ABV";
	static final String ibu = "IBU";
	static final String date = "Date";
	static final String rating = "Rating";
	static final String notes = "Notes";
	static final String picture = "Picture";
	
	
	
	public BeerDB(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS category ( " +
				"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
				"category TEXT);");
		
		
		db.execSQL("CREATE TABLE IF NOT EXISTS beer ( " +
				"_id INTEGER PRIMARY KEY AUTOINCREMENT,"  +
				"name TEXT NOT NULL, " +
				"brewery TEXT NOT NULL, " +
				"category INTEGER, " +
				"style TEXT, " +
				"abv REAL, " +
				"ibu REAL, " +
				"date TEXT, " +
				"rating REAL, " +
				"notes TEXT, " +
				"picture TEXT, " +
				"FOREIGN KEY (category) REFERENCES category(_id) );");

		//Populate our style table
		populateCategory(db);
		
		//DEVELOPMENT STATMENT!
		populateBeer(db);
	}
	
	@Override
	public void onOpen(SQLiteDatabase db)
	{
		super.onOpen(db);
		if (!db.isReadOnly()) {
			// Enable foreign key constraints
			db.execSQL("PRAGMA foreign_keys=ON;");
		}
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS beer");
		db.execSQL("DROP TABLE IF EXISTS category");
		onCreate(db);
	}
	
	//FOR DEVELOPMENT ONLY
	private void populateBeer(SQLiteDatabase db){
		db.execSQL("INSERT INTO beer VALUES(null,'Mephistopheles','Avery','25','American Stout','15.1','107',null,5,null,'avery-mephistopheles');");
		db.execSQL("INSERT INTO beer VALUES(null,'The Reverend','Avery','5','Belgian Dark Strong Ale','10','10',null,5,null,'avery-thereverend');");
	}
	
	private void populateCategory(SQLiteDatabase db){
		db.execSQL("INSERT INTO category VALUES(null,' ');");
		db.execSQL("INSERT INTO category VALUES(null,'Amber Hybrid Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'American Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'Belgian/French Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'Belgian Strong Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'Bock');");
		db.execSQL("INSERT INTO category VALUES(null,'English Brown Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'English Pale Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'European Amber Lager');");
		db.execSQL("INSERT INTO category VALUES(null,'Fruit Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'German Wheat/Rye Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'India Pale Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'Light Hybrid Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'Light Lager');");
		db.execSQL("INSERT INTO category VALUES(null,'Melomel');");
		db.execSQL("INSERT INTO category VALUES(null,'Mead');");
		db.execSQL("INSERT INTO category VALUES(null,'Pilsner');");
		db.execSQL("INSERT INTO category VALUES(null,'Porter');");
		db.execSQL("INSERT INTO category VALUES(null,'Scottish/Irish Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'Smoke-Flavored/Wood-Aged Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'Sour Ale');");
		db.execSQL("INSERT INTO category VALUES(null,'Specialty Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'Cider/Perry');");
		db.execSQL("INSERT INTO category VALUES(null,'Spice/Herb/Vegetable Beer');");
		db.execSQL("INSERT INTO category VALUES(null,'Stout');");
		db.execSQL("INSERT INTO category VALUES(null,'Strong Ale');");
	}

}
